-- vim 的原生文档树, % 创建文件, 使用 nvim-tree 或者 oil 替代 netrwPlugin
-- vim.keymap.set('', '<leader>e', '<Cmd>Lex 26<CR>', { noremap = true, silent = true })

-- 当超过编辑器的边界视觉上换行了，其实还是一行时，跳行的以实际算一行计算
-- https://www.reddit.com/r/vim/comments/2k4cbr/problem_with_gj_and_gk/
vim.keymap.set(
  "n",
  "j",
  [[v:count ? 'j' : 'gj']],
  { noremap = true, expr = true }
)
vim.keymap.set(
  "n",
  "k",
  [[v:count ? 'k' : 'gk']],
  { noremap = true, expr = true }
)

-- Use jk to exit insert mode   <Esc> <=> <C-[>
vim.keymap.set(
  "i",
  "jk",
  "<Esc>",
  { desc = "Exit insert mode with jk", noremap = true, silent = true }
)

-- File Save
vim.keymap.set({ "i", "n" }, "<C-s>", "<Esc><Cmd>write<CR>", { noremap = true })

-- Select all
vim.keymap.set("n", "<C-a>", "gg<S-v>G", { noremap = true, silent = true })

-- 光标回到行首
vim.keymap.set({ "n", "x", "o" }, "H", "^", { desc = "Cursor Start of line" })
-- 光标回到行未
vim.keymap.set({ "n", "x", "o" }, "L", "$", { desc = "Cursor End of line" })

-- clear search highlights
vim.keymap.set(
  "n",
  "<leader>nh",
  "<Cmd>nohl<CR>",
  { silent = true, noremap = true, desc = "Clear search highlights" }
)

-- window management
vim.keymap.set(
  "n",
  "<leader>sv",
  "<C-w>v",
  { desc = "Split window vertically" }
) -- split window vertically
vim.keymap.set(
  "n",
  "<leader>sh",
  "<C-w>s",
  { desc = "Split window horizontally" }
) -- split window horizontally
vim.keymap.set("n", "<leader>se", "<C-w>=", { desc = "Make splits equal size" }) -- make split windows equal width & height
vim.keymap.set(
  "n",
  "<leader>sx",
  "<cmd>close<CR>",
  { desc = "Close current split" }
) -- close current split window

vim.keymap.set({ "n", "x" }, "<S-q>", "<Cmd>:q<CR>")
vim.keymap.set({ "n", "x" }, "Q", "<Cmd>:qa<CR>")

-- ctrl + o 与 ctrl + i  来回跳

-- 关闭 buffer
vim.keymap.set(
  "n",
  "<leader>bd",
  "<cmd>bd<CR>",
  { desc = "Close current buffer" }
)

-- 使用 <Shift-u> 将光标所在单词转化为全大写
vim.keymap.set({ "i", "v", "n" }, "<S-A-u>", "vawU", { desc = "Uppercase" }) -- 光标跳到本词开始
-- vim.keymap.set({ "i", "v", "n" }, "<S-C-u>", "<ESC>viwUw") -- 光标跳到本词末尾
-- 使用 <Shift-l> 将光标所在单词转化为全小写
vim.keymap.set({ "i", "v", "n" }, "<S-A-l>", "vawu", { desc = "Lowercase" })
-- vim.keymap.set({ "i", "v", "n" }, "<S-C-l>", "<ESC>viwuw")

local sign_define_diagnstic = function(
  err,
  warn,
  hint,
  info,
  isDiagnosticsVisible
)
  local x = vim.diagnostic.severity

  vim.diagnostic.config({
    underline = isDiagnosticsVisible,
    virtual_lines = isDiagnosticsVisible, --  多行诊断信息, 可以不设置, 使用插件trouble.nvim 在 buffer 中显示更加友好
    virtual_text = {
      -- 在代码旁显示诊断信息  -- 设置诊断显示样式
      prefix = "●", -- Could be "■-> ", '●', '■', '▎', 'x'
      spacing = 4,
    },
    signs = {
      text = {
        [x.ERROR] = err or "✘",
        [x.WARN] = warn or "",
        [x.HINT] = hint or "⚑",
        [x.INFO] = info or "",
      },
    },
    severity_sort = true, -- 按严重程度排序显示
    update_in_insert = true, -- 插入模式下不更新诊断
    float = {
      source = true,
      focusable = true,
      border = "rounded",
    },
  })
end

local icon = {
  ERROR = "", -- "✘",
  WARN = "", --"",
  HINT = "⚑", -- '󰌵',
  INFO = "", -- "", -- '󰋇',
}

-- Toggle LSP diagnostics visibility
local isLspDiagnosticsVisible = false
sign_define_diagnstic(
  icon.ERROR,
  icon.WARN,
  icon.HINT,
  icon.INFO,
  isLspDiagnosticsVisible
)
vim.keymap.set("n", "<leader>lx", function()
  isLspDiagnosticsVisible = not isLspDiagnosticsVisible
  sign_define_diagnstic(
    icon.ERROR,
    icon.WARN,
    icon.HINT,
    icon.INFO,
    isLspDiagnosticsVisible
  )
end, { desc = "Toggle LSP diagnostics" })
